package app.kotlin_1.activity

import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import app.kotlin_1.R
import app.kotlin_1.adapter.SearchContentAdapter
import app.kotlin_1.application.MyApplicaiton
import app.kotlin_1.base.BaseActivity
import app.kotlin_1.base.BaseRecyclerViewAdapter
import app.kotlin_1.bean.NSearchBean
import app.kotlin_1.sql.SQLiteManager
import app.kotlin_1.sql.SearchDaoUtils
import app.kotlin_1.utils.e
import app.kotlin_1.utils.toast
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.title_back.*
import java.util.*

/**
 * Created by 刘智强 on 2017/9/20.
 * 搜索页面
 */
class SearchActivity : BaseActivity() {

    private var searchDaoUtils: SearchDaoUtils? = null
    override fun getlayoutId() = R.layout.activity_search
    private var searchContent: String? = ""
    private var searchHistoryList = mutableListOf<NSearchBean>()
    private var adapter: SearchContentAdapter? = null

    override fun getData() {
    }

    override fun initData() {
        ivDownload.visibility = View.GONE
        tvTitle.text = "搜索"
        searchDaoUtils = SearchDaoUtils(SQLiteManager.getInstangce(MyApplicaiton.getInstance()))
        rvSearchHistory.layoutManager = LinearLayoutManager(activity)
        adapter = SearchContentAdapter(activity, searchHistoryList, R.layout.item_search_history)
        rvSearchHistory.adapter = adapter
    }

    override fun initView() {
    }

    override fun initListenter() {
        ivBack.setOnClickListener(this)
        et_search.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                searchContent = s?.trim().toString()
                searchContent?.let {
                    searchHistoryList = searchDaoUtils?.queryByName(it)?.list()!!
                    adapter?.setData(searchHistoryList)
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
        tvSearch.setOnClickListener(this)
        adapter?.onItemClickListener = object : BaseRecyclerViewAdapter.OnItemClickListener {
            override fun onClick(v: View, position: Int) {
                activity?.toast(searchHistoryList[position].name)
            }

            override fun onLongClick(v: View, Position: Int) {
            }

        }

    }

    override fun requestData() {
        var sql = "order by date Desc limit 0,5"
        searchHistoryList = searchDaoUtils?.query()?.list()!!
        if (searchHistoryList.isEmpty()) {
            layNoContent.visibility = View.VISIBLE
        } else {
            layNoContent.visibility = View.GONE
            adapter?.setData(searchHistoryList)
        }
    }

    override fun setData(any: Any) {

    }

    override fun setOnClick(v: View?) {
        when (v!!.id) {
            R.id.ivBack -> finish()
            R.id.tvSearch -> {
                searchContent = et_search?.text.toString().trim()
                activity?.e("搜索的内容是$searchContent")
//                searchContent?.isEmpty()?.let {
//                    var searchBean = NSearchBean(null, searchContent, Date())
//                    if (searchDaoUtils?.insertBean(searchBean)!!) {
//                        searchHistoryList = searchDaoUtils?.query()?.list()!!
//                        adapter?.setData(searchHistoryList)
//                    }
//                }
                if (!searchContent!!.isEmpty()) {
                    var searchBean = NSearchBean(null, searchContent, Date())
                    if (searchDaoUtils?.insertBean(searchBean)!!) {
                        searchHistoryList = searchDaoUtils?.query()?.list()!!
                        if (searchHistoryList.size > 0) layNoContent.visibility = View.GONE
                        adapter?.setData(searchHistoryList)
                    }
                }

            }
        }
    }
}



